/**
 * Copyright 2013 Joan Zapata
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xunxi.mimi.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;

import java.util.HashSet;
import java.util.LinkedHashSet;


/**
 * https://github.com/CymChad/BaseRecyclerViewAdapterHelper
 */
public class BaseViewHolder extends com.chad.library.adapter.base.BaseViewHolder {

    private final LinkedHashSet<Integer> childClickViewIds;

    public BaseViewHolder(View view) {
        super(view);
        this.childClickViewIds = new LinkedHashSet<>();
    }

    public BaseViewHolder setText(int viewId, @StringRes int strId, Object... args) {
        TextView textView = getView(viewId);
        textView.setText(textView.getResources().getString(strId, args));
        return this;
    }

    public BaseViewHolder setText(int viewId, String format, Object... args) {
        TextView textView = getView(viewId);
        textView.setText(String.format(format, args));
        return this;
    }

    public BaseViewHolder setRating(int viewId, @StringRes int strId, Object... args) {
        RatingBar ratingBar = getView(viewId);
        ratingBar.setRating(Float.parseFloat(ratingBar.getResources().getString(strId, args)));
        return this;
    }

    public BaseViewHolder setText(int viewId, CharSequence value) {
        TextView view = getView(viewId);
        view.setText(value);
        return this;
    }

    public BaseViewHolder setForegroundColorSpan(int viewId, int foreColor, int start, int end) {
        TextView view = getView(viewId);
        SpannableString span = new SpannableString(view.getText().toString());
        ForegroundColorSpan foreSpan = new ForegroundColorSpan(foreColor);
        span.setSpan(foreSpan, start, view.getText().toString().length() - end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        view.setText(span);
        return this;
    }

    public BaseViewHolder setSelected(int viewId, boolean selected) {
        getView(viewId).setSelected(selected);
        return this;
    }

    public BaseViewHolder setFocusable(int viewId, boolean focusable) {
        getView(viewId).setFocusable(focusable);
        return this;
    }

    public BaseViewHolder setFocusableInTouchMode(int viewId, boolean focusable) {
        getView(viewId).setFocusableInTouchMode(focusable);
        return this;
    }

    public BaseViewHolder setHtmlText(int viewId, String text) {
        try {
            TextView textView = getView(viewId);
            textView.setText(Html.fromHtml(text));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public BaseViewHolder setTextSize(int viewId, float sizeInSp) {
        TextView textView = getView(viewId);
        textView.setTextSize(sizeInSp);
        return this;
    }

    public BaseViewHolder setTextSizeOfRes(int viewId, @DimenRes int sizeResId) {
        TextView textView = getView(viewId);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textView.getResources().getDimension(sizeResId));
        return this;
    }

    public BaseViewHolder setInVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public Context getContext() {
        return getConvertView() != null ? getConvertView().getContext() : null;
    }


//    public RadiusTextView getRadiusTextView(@IdRes int id) {
//        return getView(id);
//    }

//    public BaseViewHolder setRadiusViewBackgroundColor(@IdRes int id, int color) {
//        try {
//            getRadiusTextView(id).getDelegate().setBackgroundColor(color);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return this;
//    }

    public BaseViewHolder setImageUrl(@IdRes int id, String imageUrl) {
        try {
            ImageView view = getView(id);
            Glide.with(view.getContext())
                    .load(imageUrl)
                    .centerCrop()
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public BaseViewHolder setTextLines(@IdRes int id, int lines) {
        try {
            TextView view = getView(id);
            view.setLines(lines);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public BaseViewHolder setTextMaxLines(@IdRes int id, int maxLines) {
        try {
            TextView view = getView(id);
            view.setMaxLines(maxLines);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

//    public BaseViewHolder setStrokeColor(@IdRes int id, int color) {
//        try {
//            RadiusTextView view = getView(id);
//            view.getDelegate().setStrokeColor(color);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return this;
//    }

    public BaseViewHolder setImageRes(@IdRes int id, @DrawableRes int resId) {
        try {
            ImageView view = getView(id);
            Glide.with(view.getContext())
                    .load(resId)
                    .centerCrop()
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public BaseViewHolder setDrawableRight(@IdRes int id, @DrawableRes int drawableRes) {
        try {
            TextView textView = getView(id);
            textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public BaseViewHolder setDrawableLeft(@IdRes int id, @DrawableRes int drawableRes) {
        try {
            TextView textView = getView(id);
            textView.setCompoundDrawablesWithIntrinsicBounds(drawableRes, 0, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }
//
//    public BaseViewHolder setRTVBackgroundColor(@IdRes int id, int color) {
//        try {
//            RadiusTextView textView = getView(id);
//            textView.getDelegate().setBackgroundColor(color);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return this;
//    }

    @Override
    public BaseViewHolder setBackgroundRes(int viewId, @DrawableRes int backgroundRes) {
        View view = getView(viewId);
        if (view == null) return this;
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        int paddingTop = view.getPaddingTop();
        int paddingLeft = view.getPaddingLeft();
        int paddingRight = view.getPaddingRight();
        int paddingBottom = view.getPaddingBottom();
        view.setBackgroundResource(backgroundRes);
        view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
//        } else {
//            view.setBackgroundResource(backgroundRes);
//        }
        return this;
    }

    public BaseViewHolder setContentViewTag(Object tag) {
        convertView.setTag(tag);
        return this;
    }

    public BaseViewHolder setContentViewTag(int key, Object tag) {
        convertView.setTag(key, tag);
        return this;
    }

    public BaseViewHolder setTextDrawables(int id, int left, int top, int right, int bottom) {
        TextView textView = getView(id);
        textView.setCompoundDrawables(textView.getResources().getDrawable(left),
                textView.getResources().getDrawable(top),
                textView.getResources().getDrawable(right),
                textView.getResources().getDrawable(bottom));
//        textView.setCompoundDrawablesRelativeWithIntrinsicBounds(left, top, right, bottom);
        return this;
    }

    @Override
    public HashSet<Integer> getChildClickViewIds() {
        return childClickViewIds;
    }

    @SuppressWarnings("unchecked")
    @Override
    public BaseViewHolder addOnClickListener(final int viewId) {
        childClickViewIds.add(viewId);
        return this;
    }

    public BaseViewHolder removeOnClick(final int viewId) {
        childClickViewIds.remove(viewId);
        return this;
    }

    public BaseViewHolder setEnabled(@IdRes int id, boolean enabled) {
        getView(id).setEnabled(enabled);
        return this;
    }

    public BaseViewHolder setEnabled(boolean enabled) {
        itemView.setEnabled(enabled);
        return this;
    }

    public BaseViewHolder setTextColorRes(@IdRes int id, int colorRes) {
        TextView view = getView(id);
        view.setTextColor(ContextCompat.getColorStateList(getContext(), colorRes));
        return this;
    }

    public BaseViewHolder setActivated(@IdRes int id, boolean activated) {
        getView(id).setActivated(activated);
        return this;
    }

    @Override
    public BaseViewHolder setChecked(int viewId, boolean checked) {
        View view = getView(viewId);
        // View unable cast to Checkable
        if (view instanceof Checkable) {
            ((Checkable) view).setChecked(checked);
        }
        return this;
    }

    @Override
    public BaseViewHolder setText(int viewId, int strId) {
        return (BaseViewHolder) super.setText(viewId, strId);
    }

    @Override
    public BaseViewHolder setImageResource(int viewId, int imageResId) {
        return (BaseViewHolder) super.setImageResource(viewId, imageResId);
    }

    @Override
    public BaseViewHolder setBackgroundColor(int viewId, int color) {
        return (BaseViewHolder) super.setBackgroundColor(viewId, color);
    }

    @Override
    public BaseViewHolder setTextColor(int viewId, int textColor) {
        return (BaseViewHolder) super.setTextColor(viewId, textColor);
    }

    @Override
    public BaseViewHolder setImageDrawable(int viewId, Drawable drawable) {
        return (BaseViewHolder) super.setImageDrawable(viewId, drawable);
    }

    @Override
    public BaseViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        return (BaseViewHolder) super.setImageBitmap(viewId, bitmap);
    }

    @Override
    public BaseViewHolder setAlpha(int viewId, float value) {
        return (BaseViewHolder) super.setAlpha(viewId, value);
    }

    @Override
    public BaseViewHolder setVisible(int viewId, boolean visible) {
        return (BaseViewHolder) super.setVisible(viewId, visible);
    }

    public boolean getVisible(@IdRes int viewId) {
        return getView(viewId).getVisibility() == View.VISIBLE;
    }

    @Override
    public BaseViewHolder setTypeface(int viewId, Typeface typeface) {
        return (BaseViewHolder) super.setTypeface(viewId, typeface);
    }

    @Override
    public BaseViewHolder setTypeface(Typeface typeface, int... viewIds) {
        return (BaseViewHolder) super.setTypeface(typeface, viewIds);
    }

    @Override
    public BaseViewHolder setProgress(int viewId, int progress) {
        return (BaseViewHolder) super.setProgress(viewId, progress);
    }

    @Override
    public BaseViewHolder setProgress(int viewId, int progress, int max) {
        return (BaseViewHolder) super.setProgress(viewId, progress, max);
    }

    @Override
    public BaseViewHolder setMax(int viewId, int max) {
        return (BaseViewHolder) super.setMax(viewId, max);
    }

    @Override
    public BaseViewHolder setRating(int viewId, float rating) {

        return (BaseViewHolder) super.setRating(viewId, rating);
    }

    @Override
    public BaseViewHolder setRating(int viewId, float rating, int max) {
        return (BaseViewHolder) super.setRating(viewId, rating, max);
    }

    public<T> T getContentViewTag(int key) {
        return (T) convertView.getTag(key);
    }
}
